﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Threading;
using DiPlaner.Profile;
using DiPlaner.Properties;
using Binding = System.Windows.Data.Binding;

namespace DiPlaner
{

    public partial class MainForm
    {
        #region VARIABLES

        #region _private

        SynchronizationContext main_context;
        DispatcherTimer formTimer;
        OpenFileDialog openFileDialog;
        SaveFileDialog saveFileDialog;
        LogForm logForm;

        int idleCount, CheckCounter;
        bool firstStart = true;

        #endregion

        #region _public

        //public static readonly WorkerClass Worker = new WorkerClass();
        public static List<ListTemplate> BotLines = new List<ListTemplate>();

        public static volatile bool CheckAllow = true;
        public static bool Hooked;

        public static Stopwatch sysWatch = new Stopwatch();

        public static string _lastdb_path;
        public static string _lastd3_path;

        #endregion

        #endregion

        #region DiForm

        public MainForm()
        {
            InitializeComponent();

            main_context = new SynchronizationContext();

            formTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
            formTimer.Tick += formTimer_Tick;
            formTimer.Start();

            Closing += MainForm_Closing;
            Closed += MainForm_Closed;

            Settings.Default.Upgrade();
            if (Settings.Default.ProfileList != null)
                for (var i = 0;Settings.Default.ProfileList.Count > i && i <= 10;i++) 
                {
                    if (File.Exists(Settings.Default.ProfileList[i]))
                        comb_profileList.Items.Add(Settings.Default.ProfileList[i]);
                }
            if (Settings.Default.LastProfile != null && comb_profileList.Items.Contains(Settings.Default.LastProfile))
                LoadPlanFile(Settings.Default.LastProfile);

            _lastd3_path = !string.IsNullOrEmpty(Settings.Default.lastD3) ? Settings.Default.lastD3 : string.Empty;
            _lastdb_path = !string.IsNullOrEmpty(Settings.Default.lastDB) ? Settings.Default.lastDB : string.Empty;

            var mainHeightBinding = new Binding("mainHeight") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            var mainTopBinding = new Binding("mainTop") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            var mainLeftBinding = new Binding("mainLeft") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            SetBinding(HeightProperty, mainHeightBinding);
            SetBinding(TopProperty, mainTopBinding);
            SetBinding(LeftProperty, mainLeftBinding);

            var logHeightBinding = new Binding("logHeight") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            var logWidthBinding = new Binding("logWidth") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            var logTopBinding = new Binding("logTop") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            var logLeftBinding = new Binding("logLeft") { Source = Settings.Default, Mode = BindingMode.TwoWay };
            SetBinding(HeightProperty, logHeightBinding);
            SetBinding(WidthProperty, logWidthBinding);
            SetBinding(TopProperty, logTopBinding);
            SetBinding(LeftProperty, logLeftBinding);

        }

        void LoadPlanFile(string lastProfile)
        {
        }

        void formTimer_Tick(object sender, EventArgs e) 
        {
            lb_localTime.Content = string.Format("Local Time {0}", DateTime.Now.ToString("HH:mm:ss"));
        }

        void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
        {
            formTimer.Stop();
            //Worker.StopWork();
            //Program.StopHook();
        }

        void MainForm_Closed(object sender, EventArgs e) 
        {
            if (!string.IsNullOrEmpty((string)comb_profileList.SelectedItem))
                Settings.Default.LastProfile = (string)comb_profileList.SelectedItem;

            if (comb_profileList.Items != null && comb_profileList.Items.Count > 0) 
            {
                Settings.Default.ProfileList = new System.Collections.Specialized.StringCollection();
                foreach (string path in comb_profileList.Items)
                    Settings.Default.ProfileList.Add(path);
            }
            Settings.Default.lastD3 = _lastd3_path;
            Settings.Default.lastDB = _lastdb_path;
            if (WindowState != WindowState.Minimized)
            {
                Settings.Default.mainHeight = Height;
                Settings.Default.mainTop = Top;
                Settings.Default.mainLeft = Left;
            }
            Settings.Default.Save();
        }

        #endregion

        private void btn_FormClose_Click(object sender, RoutedEventArgs e) 
        {
            Close();
        }

        private void btn_FormMinimize_Click(object sender, RoutedEventArgs e) 
        {

        }

        private void DragWindowMove_LMB(object sender, System.Windows.Input.MouseButtonEventArgs e) 
        {
            
            DragMove();
        }

        private void btn_log_Click(object sender, RoutedEventArgs e)
        {
            if (logForm.Visible) return;
            logForm = new LogForm();
            logForm.Show();
        }
    }
}
